﻿package mortal.game.control
{
    import com.gengine.core.*;
    import flash.utils.*;
    import mortal.common.sound.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.mvc.core.*;

    public class SoundController extends Controller
    {
        private var _isFirstEnter:Boolean = true;
        private var _lastPlayEnterMusicTime:int;
        private var _bgMusicInterval:int;
        private var _enterTime:int;
        private var _bgTime:int;
        private var _bgIndex:int = 0;

        public function SoundController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.ChangeScene, this.onChangeScene);
            return;
        }// end function

        private function onChangeScene(event:DataEvent) : void
        {
            this._bgIndex = 0;
            this.closeAllMusic();
            this.delayPlayMusic(2);
            return;
        }// end function

        private function delayPlayMusic(param1:int) : void
        {
            this._bgTime = setTimeout(this.playBgMusic, param1 * 1000);
            this._enterTime = setTimeout(this.playEnterMusic, param1 * 1000);
            return;
        }// end function

        private function closeAllMusic() : void
        {
            SoundManager.instance.stopBgMusic();
            clearTimeout(this._enterTime);
            clearTimeout(this._bgTime);
            GlobalDispatcher.removeEventListener(SoundManager.BGMUSICCOMPLETE, this.onBgMusicComplete);
            GlobalDispatcher.removeEventListener(SoundManager.BGMUSICCOMPLETE2, this.onBgMusicComplete2);
            return;
        }// end function

        private function delayPlayBgMusic() : void
        {
            if (this._bgMusicInterval)
            {
                this._bgTime = setTimeout(this.playBgMusic, this._bgMusicInterval * 1000);
            }
            else
            {
                this.playBgMusic();
            }
            return;
        }// end function

        private function playEnterMusic() : void
        {
            this._lastPlayEnterMusicTime = getTimer();
            var _loc_1:* = MapFileUtil.mapInfo.enterMusic;
            if (_loc_1)
            {
                SoundManager.instance.playBgMusic(_loc_1);
                GlobalDispatcher.addEventListener(SoundManager.BGMUSICCOMPLETE, this.onBgMusicComplete);
            }
            return;
        }// end function

        protected function onBgMusicComplete(event:GlobalEvent) : void
        {
            this.playEnterMusic();
            return;
        }// end function

        private function playBgMusic() : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:String = null;
            var _loc_1:* = MapFileUtil.mapInfo.bgMusic;
            this._bgMusicInterval = MapFileUtil.mapInfo.bgMusicInterval;
            if (_loc_1)
            {
                _loc_2 = _loc_1.split("#");
                _loc_3 = this._bgIndex;
                _loc_4 = _loc_2[_loc_3];
                var _loc_5:String = this;
                _loc_5._bgIndex = this._bgIndex + 1;
                this._bgIndex = ++this._bgIndex % _loc_2.length;
                SoundManager.instance.playBgMusic2(_loc_4);
                GlobalDispatcher.addEventListener(SoundManager.BGMUSICCOMPLETE2, this.onBgMusicComplete2);
            }
            return;
        }// end function

        protected function onBgMusicComplete2(event:GlobalEvent) : void
        {
            this.delayPlayBgMusic();
            return;
        }// end function

    }
}
